package com.dragon.config;

import cn.hutool.core.io.FileUtil;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Value("${file.storage.avatar-path}")
    private String avatarPath;
    @Value("${file.storage.image-path}")
    private String imagePath;

    @PostConstruct
    public void init() {
        if(!FileUtil.exist(avatarPath)) {
            FileUtil.mkdir(avatarPath);
            log.info("头像目录不存在，创建目录: {}", avatarPath);
        }
        if(!FileUtil.exist(imagePath)) {
            FileUtil.mkdir(imagePath);
            log.info("图片目录不存在，创建目录: {}", imagePath);
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/avatar/**")
                .addResourceLocations("file:" + avatarPath);
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:" + imagePath);

        log.info("静态资源映射完成：/avatar/** -> {};{}", "file:" + avatarPath, "file:" + imagePath);
    }

}
